const router = require('koa-router')()
const vod = require('../../models/glxxvod/vod.js')
const encoding = require('encoding')
const fs = require("fs").promises
const path = require("path")
const { ok } = require('assert')

router.prefix('/course')

router.get('/test', async (ctx, next) => {
    let str1 = "ÐÅÏ¢¼¼Êõ"
    let str2 = "²âÊÔUBUNTU"
    let str3 = "abcdefghadskf"
    // var result = encoding.convert(str1, "Latin_1");
    var res = encoding.convert(str2, "UTF8", "GBK")
    //ctx.body=res.toString()
    ctx.body = gbkTOutf8(str3)
})

function gbkTOutf8(str) {
    let newstring = encoding.convert(str, "UTF8", "GBK")
    return newstring.toString();
}

router.get("/rename", async (ctx, next) => {
    // let path=ctx.query.path;
    // let name1 = ctx.query.name1;
    // let name2 = ctx.query.name2;
    // const dir = await fs.promises.opendir(path);
    // for await (const dirent of dir) {
    //     console.log(dirent.name);
    // }
    // try {
    //     //await fs.rename(path.join(__dirname, name1), path.join(__dirname, name2))
    //     await fs.rename(name1,name2)
    //     ctx.body = "改名成功";
    // } catch (err) {
    //     ctx.body = "改名失败:" + err.message
    // }
    let a="\abcd.txt";
    console.log(a.replace("\\",""))
})

router.get('/upload', async (ctx, next) => {
    let title = ctx.query.title
    let uid = ctx.query.uid;
    let subject = ctx.query.subject;
    let grade = ctx.query.grade;
    let filename = ctx.query.filename;
    let desc = ctx.query.desc;
    let cover = ctx.query.cover;
    title = gbkTOutf8(title)
    uid = gbkTOutf8(uid)
    subject = gbkTOutf8(subject)
    grade = gbkTOutf8(grade)
    filename = gbkTOutf8(filename)
    desc = gbkTOutf8(desc)
    cover = gbkTOutf8(cover)
    try {
        var data = await vod.Save({ title: title, uid: uid, subject: subject, grade: grade, filename: filename, desc: desc, cover: cover });
        await fs.rename('/home/vsftpd/hantao/\\'+cover,'/home/vsftpd/hantao/'+cover)
        await fs.rename('/home/vsftpd/hantao/\\'+filename,'/home/vsftpd/hantao/'+filename)
        ctx.body = { "error": false, "result": data };
    } catch (err) {
        ctx.body = { "error": true, "message": "保存视频记录时出错：" + err.message }
    }
})

router.get('/getlesson', async (ctx, net) => {
    let keyword = ctx.query.keyword;
    let pagesize = ctx.query.pagesize;
    let lastid = ctx.query.lastid;
    try {
        var data = await vod.myPaging(keyword, pagesize, lastid);
        ctx.body = { 'error': false, 'items': data.recordset, 'totalitems': data.count }
    } catch (err) {
        ctx.body = { 'error': true, 'message': "分页获取视频列表时出错：" + err.message }
    }
})

router.get('/grade', async (ctx, next) => {
    let grades = ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "适用各年级"]
    try {
        ctx.body = grades.join('\n')
    } catch (err) {
        ctx.body = { "error": true, "message": "获取年级数据出错：" + err.message }
    }
})

router.get('/subject', async (ctx, next) => {
    let subject = ["语文", "数学", "英语", "音乐", "美术", "体育", "信息技术", "思品", "科学", "其他"]
    try {
        ctx.body = subject.join('\n')
    } catch (err) {
        ctx.body = { "error": true, "message": "获取学科数据出错：" + err.message }
    }
})

router.get('/teacher', async (ctx, next) => {
    let teacher = ["管理员", "龙笛", "莫桂珍", "蒋俊兰", "张革", "庞雪", "谢永姣", "阳健", "刁娜", "李海波", "雷璇", "陈燕", "毛东兰", "秦萍", "沈云", "梁丽", "罗海春", "唐宵丽", "邹蔓莉", "曾怡静", "梁敏", "梁玮", "陈国伟", "廖佳敏", "王云芝", "陆冬艳", "黄英英", "赖先立", "唐芳玲", "唐文慧", "陆成玲", "谢立新", "韦凤", "周莉芝", "黄武", "胡文婷", "贺诗涵", "梁娟", "李志国", "赵长久", "邓洁", "李琴", "陈益婷", "李秋华", "石王玲", "周雨燕", "刘青"]
    try {
        ctx.body = teacher.join('\n')
    } catch (err) {
        ctx.body = { "error": true, "message": "获取教师数据出错：" + err.message }
    }
})

module.exports = router